package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataAnchorToFile extends DataAnchor implements ITag {
	public static String DEFAULT_ID = "_AnchorToFile";
		DataString file = new DataString(TagModifier.STRING_TYPE_FILE);
public DataAnchorToFile() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILE))
		{
			file = new DataString(TagModifier.STRING_TYPE_FILE);
			file.buildFromgrfxML(element);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ANCHOR_TO_FILE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILE))
		{
			file.copyFromgrfxML(element);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ANCHOR_TO_FILE,""+element.getStartLineNo()} );
		}
	}
}
public DataAnchor getClone() {

	DataAnchorToFile newAnchorToFile = new DataAnchorToFile();

	super.getClonedElements(newAnchorToFile);

	newAnchorToFile.file = file.getClone();

	return newAnchorToFile;
}
public DataAnchor getClone(String path) {

	DataAnchorToFile newAnchorToFile = new DataAnchorToFile();

	GrfxMLStoreProcessor.setCloneName( path,this,newAnchorToFile );

	super.getClonedElements(path,newAnchorToFile);

	newAnchorToFile.file = file.getClone(path);

	return newAnchorToFile;
}
public DataString getFile() {
	return file;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(file,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(file==oldObj)
		file=(DataString)newObj;
}
public void resetChanged() {
	super.resetChanged();
	file.resetChanged();
}
protected void setFile(DataString newFile) {
	file = newFile;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += file.togrfxML(Token.FILE);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
}
